<template>
  <div id="detail">
    <child-detail class="nav" @itemClick="titleClick"></child-detail>
    <scroll class="content" ref="scroll" @scroll="contentScroll">
      <detail-swiper :top-images="topImages"></detail-swiper>
      <detail-base-info :goods="goods"></detail-base-info>
      <detail-shop-info :shopinfo="shopinfo"></detail-shop-info>
      <detail-info :detailInfo="detailInfo"></detail-info>
      <params-info :paramInfo="paramInfo" ref="params"></params-info>
      <detail-comment-info
        :commentInfo="commentInfo"
        ref="comment"
      ></detail-comment-info>
      <goods-list :goods="recommends" ref="recommend"></goods-list>
      <detail-bottom-bar @addCart="addToCart"></detail-bottom-bar>
    </scroll>
    <back-top @click.native="backtopClick" v-show="isShowBacktop"></back-top>
  </div>
</template>

<script>
import { getDetail, Goods, Shop, getRecommend } from "network/detail";
import DetailSwiper from "./childdetail/DetailSwiper.vue";
import DetailBaseInfo from "./childdetail/DetailBaseInfo.vue";
import DetailShopInfo from "./childdetail/DetailShopInfo.vue";
import ChildDetail from "views/detail/childdetail/ChildDetail.vue";
import Scroll from "components/common/scroll/Scroll.vue";
import DetailInfo from "./childdetail/DetailInfo.vue";
import ParamsInfo from "./childdetail/ParamsInfo.vue";
import DetailCommentInfo from "./childdetail/DetailCommentInfo.vue";
import GoodsList from "components/content/goods/GoodsList.vue";
import DetailBottomBar from "./childdetail/DetailBottomBar.vue";
import BackTop from "components/content/backTop/BackTop.vue";

export default {
  components: {
    ChildDetail,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailInfo,
    ParamsInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    BackTop,
    ChildDetail,
  },
  name: "Detail",
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shopinfo: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopys: [0, 1000, 2000, 3000],
      isShowBacktop: false,
    };
  },
  methods: {
    titleClick(index) {
      // console.log(index);
      this.$refs.scroll.scrollTo(0, -this.themeTopys[index], 200);
    },
    backtopClick() {
      this.$refs.scroll.scrollTo(0, 0, 500);
    },
    contentScroll(position) {
      // 1.判断BackTop是否显示
      this.isShowBacktop = -position.y > 600;
      // 2.判断tabContral是否吸顶
      this.istabFixed = -position.y > this.tabOffsetTop;
    },
    addToCart() {
      // 1.获取购物车需要展示的信息
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid;

      // 2.将商品添加到购物车里
      this.$store.commit('addCart',product)
    },
  },
  created() {
    // 1.保存存入的iid
    this.iid = this.$route.params.id;
    // 2.根据iid请求信息数据
    getDetail(this.iid).then((res) => {
      // console.log(res);
      // 2.1获取顶部的图片数据
      const re = res.result;
      this.topImages = re.itemInfo.topImages;
      //2.2 获取商品信息
      this.goods = new Goods(re.itemInfo, re.columns, re.shopInfo.services);
      // 2.3获取商家信息
      this.shopinfo = new Shop(re.shopInfo);
      //2.4获取详情图片
      this.detailInfo = re.detailInfo;
      // 2.5获取参数信息
      this.paramInfo = re.itemParams;
      // 2.6获取评论信息
      if (re.rate.cRate !== 0) {
        this.commentInfo = re.rate;
      }
      // this.$nextTick(() => {
      //   this.themeTopys.push(0);
      //   this.themeTopys.push(this.$refs.params.$el.offsetTop)
      //   this.themeTopys.push(this.$refs.comment.$el.offsetTop)
      //   this.themeTopys.push(this.$refs.recommend.$el.offsetTop)
      // })
    });
    // 3.请求推荐数据
    getRecommend().then((res) => {
      console.log(res);
      this.recommends = res.data.list;
    });
  },
};
</script>

<style scoped>
#detail {
  position: relative;
  z-index: 10;
  background-color: #fff;
  height: 100vh;
}
.content {
  height: calc(100% - 44px);
}
.nav {
  background-color: #fff;
  position: relative;
  z-index: 8;
}
</style>